package com.alinesno.infra.smart.assistant.scene.scene.documentReader.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alinesno.infra.common.core.service.impl.IBaseServiceImpl;
import com.alinesno.infra.common.facade.datascope.PermissionQuery;
import com.alinesno.infra.common.facade.pageable.DatatablesPageBean;
import com.alinesno.infra.smart.assistant.scene.scene.documentReader.enums.DocParserStatusEnums;
import com.alinesno.infra.smart.assistant.scene.scene.documentReader.mapper.DocReaderTaskMapper;
import com.alinesno.infra.smart.assistant.scene.scene.documentReader.service.IDocReaderTaskService;
import com.alinesno.infra.smart.scene.entity.DocReaderTaskEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通用助手任务服务实现类
 */
@Slf4j
@Service
public class DocReaderTaskServiceImpl extends IBaseServiceImpl<DocReaderTaskEntity, DocReaderTaskMapper> implements IDocReaderTaskService {

    @Override
    public List<DocReaderTaskEntity> pagerListByPage(DatatablesPageBean page, PermissionQuery query , Long sceneId) {

        page.setPageNum(0);
        page.setPageSize(20);

        Page<DocReaderTaskEntity> pageBean = new Page<>(page.getPageNum(), page.getPageSize());

        LambdaQueryWrapper<DocReaderTaskEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocReaderTaskEntity::getOrgId, query.getOrgId()) ;
        wrapper.eq(DocReaderTaskEntity::getSceneId, sceneId);
        wrapper.orderByDesc(DocReaderTaskEntity::getAddTime) ;

        pageBean = page(pageBean, wrapper);

        return pageBean.getRecords();
    }

    @Override
    public DocReaderTaskEntity addTask(String fileName, String storageId, String fileSuffix, Long sceneId , PermissionQuery query) {

        DocReaderTaskEntity entity = new DocReaderTaskEntity() ;
        BeanUtils.copyProperties(query , entity);

        entity.setTaskName(fileName);
        entity.setSceneId(sceneId);

        entity.setDocumentId(storageId);
        entity.setDocumentName(fileName);
        entity.setVectorStoreName(IdUtil.nanoId(10));  // 向量库名称
        entity.setChannelStreamId(IdUtil.getSnowflakeNextId());

        entity.setDocumentParseStatus(DocParserStatusEnums.NOT_GENERATED.getCode());
        save(entity);

        // 生成文件向量化线程

        return entity ;
    }

}